export const loadJS = (src: string) => {
    return new Promise((resolve, reject) => {
        let script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = src;
        document.head.appendChild(script);
        script.onload = resolve;
        script.onerror = reject;
    });
}

// 日期格式化 例如：1949-10-01
export const getDateStr = (date: string | number | Date = new Date(), yearOffset = 0, monthOffset = 0, dayOffset = 0, str = '-') => {
    if (!date) {
        return '';
    }
    const d = new Date(date);
    d.setHours(d.getHours() + 8);
    d.setDate(d.getDate() + dayOffset);
    d.setMonth(d.getMonth() + monthOffset);
    d.setFullYear(d.getFullYear() + yearOffset);
    return d.toISOString().slice(0, 10).replace(/-/g, str);
}

// 时间格式化 例如：1949-10-01 10:00:00
export const getDateAndTimeStr = (date = new Date(), hourOffset = 8) => {
    if (!date) {
        return '';
    }
    const d = new Date(date);
    d.setHours(d.getHours() + hourOffset);
    const e = d.toISOString().split('T');
    return `${e[0]} ${e[1].split('.')[0]}`;
}